package cn.anc.aonicardv.util;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.util.DisplayMetrics;

import java.util.Locale;

import cn.anc.aonicardv.MyApplication;
import cn.anc.aonicardv.util.ui.CarSettingGuiHandler;
import cn.anc.httpcontrolutil.CarControl;
import cn.anc.httpcontrolutil.CarControlSettings;

/**
 * Created by yangdai on 2018/3/12.
 */

public class AppUtils {

    public static boolean appIsInstall(Context context, String packageName) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return packageInfo == null ? false : true;
    }

    public static void updateAppLanguage(Context context, Locale locale) {
        Configuration configuration = context.getResources().getConfiguration();
        configuration.locale = locale;
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        context.getResources().updateConfiguration(configuration, metrics);
    }

    @TargetApi(Build.VERSION_CODES.N)
    public static Context updateResources(Context context,Locale locale) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(locale);
        configuration.setLocales(new LocaleList(locale));
        return context.createConfigurationContext(configuration);
    }
    public static void addSetting() {
        addSystemSetting();
        addRecorderMsgSetting();
        addWifiSetting();
        addAPPSetting();
    }

    private static void addAPPSetting() {
        LogUtil.e("llcTest0215","-------:addAPPSetting");
        CarControlSettings.SettingItem s10 = new CarControlSettings.SettingItem();
        s10.settingItemKey = CarSettingGuiHandler.SETTING_ITEM_APP_LANGUAGE;
        CarControlSettings.getAllSettingItems().add(s10);

        CarControlSettings.SettingItem s11 = new CarControlSettings.SettingItem();
        s11.settingItemKey = CarSettingGuiHandler.SETTING_ITEM_APP_HELP;
        CarControlSettings.getAllSettingItems().add(s11);

        CarControlSettings.SettingItem s12 = new CarControlSettings.SettingItem();
        s12.settingItemKey = CarSettingGuiHandler.SETTING_ITEM_APP_CLEAR_CACHE;
        CarControlSettings.getAllSettingItems().add(s12);

        CarControlSettings.SettingItem s13 = new CarControlSettings.SettingItem();
        s13.settingItemKey = CarSettingGuiHandler.SETTING_ITEM_APP_ABOUT;
        CarControlSettings.getAllSettingItems().add(s13);

    }

    private static void addSystemSetting() {
        CarControlSettings.SettingItem s2 = new CarControlSettings.SettingItem();
        s2.settingItemKey = CarSettingGuiHandler.SETTING_ITEM_RESTORE_FACTORY_SETTINGS;
        CarControlSettings.getAllSettingItems().add(s2);
    }

    private static void addRecorderMsgSetting() {
        CarControlSettings.SettingItem s6 = new CarControlSettings.SettingItem();
        s6.settingItemKey = CarSettingGuiHandler.SETTING_ITEM_FORMAT_SD_CARD;
        CarControlSettings.SettingItem s = new CarControlSettings.SettingItem();
        s.settingItemKey = CarSettingGuiHandler.SETTING_ITEM_FW_VERSION;
        s.settingItemValue = CarControl.DeviceInfo.fw_version;

        CarControlSettings.SettingItem s3 = new CarControlSettings.SettingItem();
        s3.settingItemKey = CarSettingGuiHandler.SETTING_ITEM_SD_CARD_SIZE;
        s3.settingItemValue = "";
        CarControlSettings.SettingItem s5 = new CarControlSettings.SettingItem();
        s5.settingItemKey = CarSettingGuiHandler.SETTING_ITEM_APP_VERSION;
        try {
            s5.settingItemValue = "V " + MyApplication.getInstance().getPackageManager().getPackageInfo(MyApplication.getInstance().getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        CarControlSettings.getAllSettingItems().add(s3);
        CarControlSettings.getAllSettingItems().add(s6);
        CarControlSettings.getAllSettingItems().add(s);
        CarControlSettings.getAllSettingItems().add(s5);
    }

    private static void  addWifiSetting() {
        CarControlSettings.SettingItem s = new CarControlSettings.SettingItem();
        s.settingItemKey = CarSettingGuiHandler.SETTING_ITEM_WIFI_PASSWORD_MODIFY;
        s.settingItemValue = "";
        CarControlSettings.getAllSettingItems().add(s);
    }


}
